package com.beisita.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.beisita.dao.entity.StuEntity;
import com.beisita.dao.mapper.StuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:roger
 * @Date: 2020/9/27
 * @Time: 19:22
 */
@Service
public class StuService {

    @Autowired
    private StuMapper stuMapper;

    public StuEntity queryById(Long id) {
        return stuMapper.queryById(id);
    }

    public List<StuEntity> queryByPage(Map<String, Object> map) {
        return stuMapper.queryByPage(map);
    }

    public List<StuEntity> queryByOptPage(Map<String, Object> map) {
        return stuMapper.queryByOptPage(map);
    }

    public void insert(StuEntity stuEntity) {
        stuMapper.insert(stuEntity);
    }

    /**
     * 根据id修改用户
     *
     * @param stuEntity
     */
    public void updateById(StuEntity stuEntity) {
        stuMapper.updateById(stuEntity);
    }

    /**
     * 根据entity条件修改用户
     *
     * @param stuEntity
     */
    public void update(StuEntity stuEntity) {
        stuMapper.update(stuEntity, new QueryWrapper<StuEntity>().eq("name", "朝雾轻寒"));
    }

    /**
     * 根据id查询用户
     *
     * @param id
     */
    public void selectById(Long id) {
        stuMapper.selectById(id);
    }

    /**
     * 根据id查询用户
     *
     * @param stuEntity
     */
    public int selectCount(StuEntity stuEntity) {
        int count = stuMapper.selectCount(new QueryWrapper<StuEntity>().eq("sex", "男"));
        return count;
    }

    /**
     * 根据 entity 条件，查询一条记录，返回的是实体
     * 如果表内有两条或以上的相同数据则会报错，可以用来判断某类数据是否已存在
     *
     * @param stuEntity
     * @return
     */
    public StuEntity selectOne(StuEntity stuEntity) {
        QueryWrapper<StuEntity> queryWrapper = new QueryWrapper<StuEntity>();
        StuEntity stu = new StuEntity();
        stu.setName("朝雾轻寒");
        queryWrapper.setEntity(stu);
        stu = stuMapper.selectOne(queryWrapper);
        return stu;
    }

    /**
     * 根据entity条件查询返回第一个字段的值（返回id列表）
     */
    List<Object> selectObjs(StuEntity stuEntity) {
        QueryWrapper<StuEntity> queryWrapper = new QueryWrapper<StuEntity>();
        StuEntity stu = new StuEntity();
        stu.setName("男");
        queryWrapper.setEntity(stu);
        List<Object> objs = stuMapper.selectObjs(queryWrapper);
        return objs;
    }

    /**
     * 根据map条件查询返回多条数据
     */
    List<StuEntity> selectByMap(StuEntity stuEntity) {
        QueryWrapper<StuEntity> queryWrapper = new QueryWrapper<StuEntity>();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", stuEntity.getName());
        List<StuEntity> list = stuMapper.selectByMap(map);
        return list;
    }

    /**
     * 根据map条件查询返回多条数据
     */
    List<StuEntity> selectList(StuEntity stuEntity) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("stu_sex", "男");
        List<StuEntity> list = stuMapper.selectList(new QueryWrapper<StuEntity>().allEq(map));
        return list;
    }

    /**
     * 分页查询
     */
    List<Map<String, Object>> selectMapsPage(StuEntity stuEntity) {
        Page<Map<String, Object>> page = stuMapper.selectMapsPage(new Page<>(1, 5), new QueryWrapper<StuEntity>().eq("user_sex", "男"));
        return page.getRecords();
    }

    /**
     * 分页查询
     */
    StuEntity selectPage(StuEntity stuEntity) {
        Page<StuEntity> page = stuMapper.selectPage(new Page<>(1, 5), new QueryWrapper<StuEntity>().eq("user_sex", "男"));
        return page.getRecords().get(0);
    }

    /**
     * 根据ID批量查询
     *
     * @param stuEntity
     * @return
     */
    List<StuEntity> selectBatchIds(StuEntity stuEntity) {
        List<Long> ids = new ArrayList<Long>();
        ids.add(1L);
        ids.add(2L);
        ids.add(3L);
        List<StuEntity> list = stuMapper.selectBatchIds(ids);
        return list;
    }

    /**
     * 根据id删除用户
     * stuMapper.deleteById(1);
     * 根据entity条件删除用户
     * stuMapper.delete(new QueryWrapper<StuEntity>().eq("id", 1));
     * 根据map条件删除用户
     * Map<String, Object> map=new HashMap<String, Object>();
     * map.put("user_name", "zwqh");
     * map.put("user_sex","男");
     * stuMapper.deleteByMap(map);
     * 根据ID批量删除
     * List<Long> ids=new ArrayList<Long>();
     * ids.add(1L);
     * ids.add(2L);
     * ids.add(3L);
     * stuMapper.deleteBatchIds(ids);
     * 主键ID列表(不能为 null 以及 empty)
     */


    @Cacheable("stuList") // 标志读取缓存操作，如果缓存不存在，则调用目标方法，并将结果放入缓存
    public List<StuEntity> getAll() {
        System.out.println("缓存不存在，执行方法");
        return stuMapper.getAll();
    }

    @Cacheable(cacheNames = {"stu"}, key = "#id")//如果缓存存在，直接读取缓存值；如果缓存不存在，则调用目标方法，并将结果放入缓存
    public StuEntity getOne(Long id) {
        System.out.println("缓存不存在，执行方法");
        return stuMapper.getOne(id);
    }

    @CachePut(cacheNames = {"stu"}, key = "#stu.id")//写入缓存，key为stu.id,一般该注解标注在新增方法上
    public void insertStu(StuEntity stu) {
        System.out.println("写入缓存");
        stuMapper.insertStu(stu);
    }

    @CacheEvict(cacheNames = {"stu"}, key = "#stu.id")//根据key清除缓存，一般该注解标注在修改和删除方法上
    public void updateStu(StuEntity stu) {
        System.out.println("清除缓存");
        stuMapper.updateStu(stu);
    }

    @CacheEvict(cacheNames = {"stu"}, key = "#id")//根据key清除缓存，一般该注解标注在修改和删除方法上
    public void deleteStu(Long id) {
        System.out.println("清除缓存");
        stuMapper.deleteStu(id);
    }
}

